class Person {}
class Dog {}

const p1: Person = new Person();

// typeof Person: 构造函数具体类型
// InstanceType: 构造函数创建出来的实例对象的类型
type HYPerson = InstanceType<typeof Person>;
const p2: HYPerson = new Person();

function factory<T extends new (...args: any[]) => any>(
  ctor: T
): InstanceType<T> {
  return new ctor();
}

function factory1<T extends new (...args: any[]) => any>(
  ctor: T
): MyInstanceType<T> {
  return new ctor();
}

const p3 = factory(Person);
const dog = factory(Dog);

type MyInstanceType<T extends new (...args: any[]) => any> = T extends new (
  ...args: any[]
) => infer R
  ? R
  : never;

const p31 = factory1(Person);
const dog1 = factory1(Dog);

export {};
